import { HttpError, toSuccess, useTransaction } from "@sker/core"
import { SysAddon } from "@sker/entities"
import { useReqBody } from "@sker/runtime"
import { z } from "zod"

export default async () => {
    const input = z.object({
        name: z.string(),
        setting: z.any()
    })
    const body = input.parse(useReqBody())
    await useTransaction(async m => {
        const sysAddon = await m.findOne(SysAddon, { where: { name: body.name } })
        if (!sysAddon) throw new HttpError('模块未安装或已卸载')
        const setting = sysAddon.setting || {};
        await m.update(SysAddon, sysAddon.id, {
            setting: {
                ...setting,
                ...body.setting
            }
        })
    })
    return toSuccess({})
}
